﻿using System;
using System.Collections.Generic;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;

namespace SKN.SPMOSS
{
    public static class PageLayouts
    {
        /// <summary>
        /// Use this method to swap page layouts of all the items in the given list
        /// </summary>
        /// <param name="web">SPWeb object</param>
        /// <param name="listName">List / Library UI name</param>
        /// <param name="oldPageLayoutName"></param>
        /// <param name="newPageLayoutName"></param>
        /// <example>PageLayouts.SwapPageLayout(web, "Pages", "WelcomeSplash.aspx", "ArticleLinks.aspx");</example>
        public static void SwapPageLayout (SPWeb web, string listName, string oldPageLayoutName, string newPageLayoutName)
        {
            PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web);

            PageLayout[] layouts = pubWeb.GetAvailablePageLayouts();
            PageLayout newLayout = null;
            PageLayout oldLayout = null;

            foreach (PageLayout layout in layouts)
            {
                if (string.Equals(layout.Name, newPageLayoutName, StringComparison.InvariantCultureIgnoreCase))
                {
                    newLayout = layout;
                }

                if (string.Equals(layout.Name, oldPageLayoutName, StringComparison.InvariantCultureIgnoreCase))
                {
                    oldLayout = layout;
                }
            }

            SwapPageLayout(web, oldLayout, newLayout);
        }

        public static void SwapPageLayout (SPWeb web, PageLayout oldLayout, PageLayout newLayout)
        {
            string checkInComment = "PageLayout updated via utility.";

            if (PublishingWeb.IsPublishingWeb(web))
            {
                PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web);

                foreach (SPListItem listItem in pubWeb.PagesList.Items)
                {
                    if (PublishingPage.IsPublishingPage(listItem))
                    {
                        PublishingPage page = PublishingPage.GetPublishingPage(listItem);
                        if (string.Equals(page.Layout.Name, oldLayout.Name, StringComparison.InvariantCultureIgnoreCase))
                        {
                            page.CheckOut();
                            page.Layout = newLayout;
                            page.Update();
                            page.CheckIn(checkInComment);
                            SPFile pageFile = page.ListItem.File;

                            pageFile.Publish(checkInComment);


                            if (pubWeb.PagesList.EnableModeration)
                            {
                                if (listItem.ModerationInformation.Status.Equals(SPModerationStatusType.Pending))
                                {
                                    pageFile.Approve(checkInComment);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
